<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>图片懒加载</title>
</head>
<body>
    <div class="wrap-box">
        <div class="w-100">
            <a href="index.html">懒加载</a>
            <a href="not-lazy.html">非懒加载</a>
            <a href="scroll-lazy.html">滚动条懒加载</a>
        </div>
        <img data-src="images/architecture-768432_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/castle-gd39222771_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/fall-g823587ea5_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/hintersee-3601004_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/kuala-lumpur-1820944_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/lighthouse-6581129_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/mountains-1761292_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/skater-g105217e59_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/tianjin-2185510_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/architecture-768432_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/castle-gd39222771_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/fall-g823587ea5_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/hintersee-3601004_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/kuala-lumpur-1820944_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/lighthouse-6581129_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/mountains-1761292_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/skater-g105217e59_1920.jpg" alt="图片" width="49.8%">
        <img data-src="images/tianjin-2185510_1920.jpg" alt="图片" width="49.8%">
        <div class="text-foot">到底了。。。</div>
    </div>
</body>
</html>
<style>
    .wrap-box{
        width: 80%;
        margin: 0 auto;
        display: flex;
        flex-wrap: wrap;
        justify-content: space-between;
    }
    .wrap-box img{
        min-height: 400px;
        margin-top: 3px;
    }
    .w-100{
        width: 100%;
    }
</style>
<script>
        let imgDoms = Array.from(document.querySelectorAll('img'));
        let io = new IntersectionObserver(function (entries) {
            entries.forEach(item => {
                if (item.intersectionRatio>0.5) {
                    console.log(item.intersectionRatio)
                    let dom = item.target;
                    let dataSrc = item.target.getAttribute('data-src');
                    if (dataSrc) {
                        dom.src = dataSrc;
                        io.unobserve(dom)//停止观察
                    }
                }

            })
        }, {
            threshold: [0.5]
        })
        imgDoms.forEach(item => {
            io.observe(item)//开始观察
        })
</script>